

package com.zdkj.subway.stm.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zdkj.common.core.util.R;
import com.zdkj.common.log.annotation.SysLog;
import com.zdkj.common.security.service.PigxUser;
import com.zdkj.common.security.util.SecurityUtils;
import com.zdkj.subway.stm.dvo.SwStReportqueryStationincomeExcelDvo;
import com.zdkj.subway.stm.entity.SwStPettymoneyCheck;
import com.zdkj.subway.stm.entity.SwStTicketCheck;
import com.zdkj.subway.stm.enums.InvoiceChechAuditTypeEnum;
import com.zdkj.subway.stm.service.SwStPettymoneyCheckService;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;


/**
 * 车站备用金盘点
 *
 * @author cxp
 * @date 2024-11-16 10:21:52
 */
@RestController
@AllArgsConstructor
@RequestMapping("/swstpettymoneycheck")
@Api(value = "swstpettymoneycheck", tags = "车站备用金盘点管理")
public class SwStPettymoneyCheckController {

    private final SwStPettymoneyCheckService swStPettymoneyCheckService;

    /**
     * 分页查询
     *
     * @param page                分页对象
     * @param swStPettymoneyCheck 车站备用金盘点
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page")
    public R getSwStPettymoneyCheckPage(Page page, SwStPettymoneyCheck swStPettymoneyCheck) {
        LambdaQueryWrapper<SwStPettymoneyCheck> queryWrapper = Wrappers.query(swStPettymoneyCheck).lambda();
        queryWrapper.orderByDesc(SwStPettymoneyCheck::getStatisticsDate).orderByAsc(SwStPettymoneyCheck::getStationCode);
        if(StrUtil.isNotBlank(swStPettymoneyCheck.getStatisticsDateStr())) {
            queryWrapper.likeRight(SwStPettymoneyCheck::getStatisticsDate, swStPettymoneyCheck.getStatisticsDateStr());
        }
        if (!SecurityUtils.getUser().getBelongToLine()) {
            queryWrapper.eq(SwStPettymoneyCheck::getLineCode, SecurityUtils.getUser().getLineCode());
            queryWrapper.eq(SwStPettymoneyCheck::getStationCode, SecurityUtils.getUser().getStationCode());
        }		
        return R.ok(swStPettymoneyCheckService.page(page, queryWrapper));
    }


    /**
     * 通过id查询车站备用金盘点
     *
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}")
    public R getById(@PathVariable("id") Long id) {
        return R.ok(swStPettymoneyCheckService.getById(id));
    }

    /**
     * 新增车站备用金盘点
     *
     * @param swStPettymoneyCheck 车站备用金盘点
     * @return R
     */
    @ApiOperation(value = "新增车站备用金盘点", notes = "新增车站备用金盘点")
    @SysLog("新增车站备用金盘点")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('stm_swstpettymoneycheck_add')")
    public R save(@RequestBody SwStPettymoneyCheck swStPettymoneyCheck) {
        return R.ok(swStPettymoneyCheckService.save(swStPettymoneyCheck));
    }

    /**
     * 修改车站备用金盘点
     *
     * @param swStPettymoneyCheck 车站备用金盘点
     * @return R
     */
    @ApiOperation(value = "修改车站备用金盘点", notes = "修改车站备用金盘点")
    @SysLog("修改车站备用金盘点")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('stm_swstpettymoneycheck_edit')")
    public R updateById(@RequestBody SwStPettymoneyCheck swStPettymoneyCheck) {
        return R.ok(swStPettymoneyCheckService.updateById(swStPettymoneyCheck));
    }

    /**
     * 通过id删除车站备用金盘点
     *
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除车站备用金盘点", notes = "通过id删除车站备用金盘点")
    @SysLog("通过id删除车站备用金盘点")
    @DeleteMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('stm_swstpettymoneycheck_del')")
    public R removeById(@PathVariable Long id) {
        return R.ok(swStPettymoneyCheckService.removeById(id));
    }

    /**
     * 通过id批量删除车站备用金盘点
     *
     * @param idList id
     * @return R
     */
    @ApiOperation(value = "通过id批量删除车站备用金盘点", notes = "通过id批量删除车站备用金盘点")
    @SysLog("通过id批量删除车站备用金盘点")
    @PostMapping("/batchDelete")
    @PreAuthorize("@pms.hasPermission('stm_swstpettymoneycheck_batch_del')")
    public R batchRemoveByIds(@RequestBody List<Integer> idList) {
        idList.forEach(swStPettymoneyCheckService::removeById);
        return R.ok();
    }

    /**
     * 通过日期查询当月备用金盘点数据
     *
     * @param checkTime
     * @return R
     */
    @ApiOperation(value = "通过日期查询当月备用金盘点数据", notes = "通过日期查询当月备用金盘点数据")
    @GetMapping("/getPettymoneyCheck")
    public R getTicketCheck(String checkTime) {
        PigxUser user = SecurityUtils.getUser();
        LambdaQueryWrapper<SwStPettymoneyCheck> queryWrapper = Wrappers.<SwStPettymoneyCheck>lambdaQuery()
                .eq(SwStPettymoneyCheck::getLineCode, user.getLineCode())
                .eq(SwStPettymoneyCheck::getStationCode, user.getStationCode())
                .eq(SwStPettymoneyCheck::getDelFlag, "0")
                .eq(SwStPettymoneyCheck::getStatisticsDate, checkTime);
                //.notIn(SwStPettymoneyCheck::getCheckStatus,2,4,6);
        return R.ok(swStPettymoneyCheckService.getMap(queryWrapper));
    }
	   /**
     * 通过日期查询当月备用金盘点数据
     *
     * @param checkTime
     * @return R
     */
    @ApiOperation(value = "通过日期查询当月备用金盘点数据", notes = "通过日期查询当月备用金盘点数据")
    @GetMapping("/getPettymoneyCheckEdit")
    public R getTicketCheckEdit(int id,String checkTime) {
        PigxUser user = SecurityUtils.getUser();
        LambdaQueryWrapper<SwStPettymoneyCheck> queryWrapper = Wrappers.<SwStPettymoneyCheck>lambdaQuery()
                .eq(SwStPettymoneyCheck::getLineCode, user.getLineCode())
                .eq(SwStPettymoneyCheck::getStationCode, user.getStationCode())
                .eq(SwStPettymoneyCheck::getDelFlag, "0")
                .eq(SwStPettymoneyCheck::getStatisticsDate, checkTime)
			    .ne(SwStPettymoneyCheck::getId,id);
                //.notIn(SwStPettymoneyCheck::getCheckStatus,2,4,6);
        return R.ok(swStPettymoneyCheckService.getMap(queryWrapper));
    }

    /**
     * 新增或修改车站备用金盘点
     *
     * @param swStPettymoneyCheck 车站备用金盘点
     * @return R
     */
    @ApiOperation(value = "新增或修改车站备用金盘点", notes = "新增或修改车站备用金盘点")
    @SysLog("新增或修改车站备用金盘点")
    @PostMapping("saveOrUpdate")
    @PreAuthorize("@pms.hasPermission('stm_swstpettymoneycheck_add_or_update')")
    public R saveOrUpdate(@RequestBody SwStPettymoneyCheck swStPettymoneyCheck) {
        return R.ok(swStPettymoneyCheckService.saveOrUpdatePettymoneyCheck(swStPettymoneyCheck));
    }

    /**
     * 备用金盘点数据批量提交
     */
    @ApiOperation(value = "备用金盘点数据批量提交", notes = "备用金盘点数据批量提交")
    @PutMapping("/modify/submit/status")
    @PreAuthorize("@pms.hasPermission('stm_swstpettymoneycheck_batch_submit')")
    public R updateSubmitStatus(@RequestParam("ids") List<Integer> ids, @RequestParam("status") String status) {
        return R.ok(swStPettymoneyCheckService.updateApprovalStatus(ids, status, null));
    }

    /**
     * 备用金盘点数据批量审核
     */
    @ApiOperation(value = "备用金盘点数据批量审核", notes = "备用金盘点数据批量审核")
    @PutMapping("/modify/approval/status")
    @PreAuthorize("@pms.hasPermission('stm_swstpettymoneycheck_batch_approval')")
    public R updateApprovalStatus(@RequestParam("ids") List<Integer> ids, @RequestParam("status") String status) {
        return R.ok(swStPettymoneyCheckService.updateApprovalStatus(ids, status, InvoiceChechAuditTypeEnum.AUDIT_STATION.getCode()));
    }

    /**
     * 备用金盘点数据批量驳回
     */
    @ApiOperation(value = "备用金盘点数据批量驳回", notes = "备用金盘点数据批量驳回")
    @PutMapping("/modify/reject/status")
    @PreAuthorize("@pms.hasPermission('stm_swstpettymoneycheck_batch_reject')")
    public R updateRejectStatus(@RequestParam("ids") List<Integer> ids, @RequestParam("status") String status) {
        return R.ok(swStPettymoneyCheckService.updateApprovalStatus(ids, status, null));
    }

    /**
     * 备用金盘点数据车间票务专业审核
     */
    @ApiOperation(value = "备用金盘点数据车间票务专业审核", notes = "备用金盘点数据车间票务专业审核")
    @PutMapping("/modify/workshopApproval/status")
    @PreAuthorize("@pms.hasPermission('stm_swstpettymoneycheck_workshop_approval')")
    public R updateWorkshopApproval(@RequestParam("ids") List<Integer> ids, @RequestParam("status") String status) {
        return R.ok(swStPettymoneyCheckService.updateApprovalStatus(ids, status, InvoiceChechAuditTypeEnum.AUDIT_WORKSHOP.getCode()));
    }

    /**
     * 备用金盘点数据车间票务专业驳回
     */
    @ApiOperation(value = "备用金盘点数据车间票务专业驳回", notes = "备用金盘点数据车间票务专业驳回")
    @PutMapping("/modify/workshopReject/status")
    @PreAuthorize("@pms.hasPermission('stm_swstpettymoneycheck_workshop_reject')")
    public R updateWorkshopReject(@RequestParam("ids") List<Integer> ids, @RequestParam("status") String status) {
        return R.ok(swStPettymoneyCheckService.updateApprovalStatus(ids, status, null));
    }

    /**
     * 备用金盘点数据部门票务专业审核
     */
    @ApiOperation(value = "备用金盘点数据部门票务专业审核", notes = "备用金盘点数据部门票务专业审核")
    @PutMapping("/modify/departmentApproval/status")
    @PreAuthorize("@pms.hasPermission('stm_swstpettymoneycheck_department_approval')")
    public R updateDepartmentApproval(@RequestParam("ids") List<Integer> ids, @RequestParam("status") String status) {
        return R.ok(swStPettymoneyCheckService.updateApprovalStatus(ids, status, InvoiceChechAuditTypeEnum.AUDIT_DEPT.getCode()));
    }

    /**
     * 备用金盘点数据部门票务专业驳回
     */
    @ApiOperation(value = "备用金盘点数据部门票务专业驳回", notes = "备用金盘点数据部门票务专业驳回")
    @PutMapping("/modify/departmentReject/status")
    @PreAuthorize("@pms.hasPermission('stm_swstpettymoneycheck_department_reject')")
    public R updateDepartmentReject(@RequestParam("ids") List<Integer> ids, @RequestParam("status") String status) {
        return R.ok(swStPettymoneyCheckService.updateApprovalStatus(ids, status, null));
    }

    /**
     * 车站导出备用金盘点表
     * @param response
     */
    @PostMapping("/exportPettymoneyCheckForStation")
    //@SysLog("车站导出备用金盘点表")
    public void exportPettymoneyCheckForStation(@RequestBody Long id, HttpServletResponse response) {
        swStPettymoneyCheckService.exportPettymoneyCheckForStation(id, response);
    }

	 /**
     * 车站导出备用金盘点表
     * @param response
     */
    @PostMapping("/exportPettymoneyCheckForStationAll")
    //@SysLog("车站导出备用金盘点表")
    public void exportPettymoneyCheckForStation(@RequestBody String  checkTime, HttpServletResponse response) {
        swStPettymoneyCheckService.exportPettymoneyCheckForStationAll(checkTime, response);
    }

    /**
     * 审核页面导出备用金盘点表
     * @param response
     */
    @PostMapping("/exportPettymoneyCheckForApproval")
    //@SysLog("审核页面导出备用金盘点表")
    public void exportPettymoneyCheckForApproval(@RequestBody SwStPettymoneyCheck swStPettymoneyCheck, HttpServletResponse response) {
        swStPettymoneyCheckService.exportPettymoneyCheckForApproval(swStPettymoneyCheck, response);
    }

}
